package com.daqian.chance.common.util;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 地理坐标位置服务
 * @Author: daqian
 * @Date: 2018/12/5 10:23
 */
public class LBSUtil {
    /**
     * 腾讯位置服务接口URL
     */
    private final static String TENCENT_LBS_URL = "https://apis.map.qq.com/ws/geocoder/v1/?key={key}&location={location}";

    /**
     * 根据经纬度获取详细地址
     * 示例：{"status":0,"message":"query ok","request_id":"339e2dbc-f834-11e8-bf4a-6c92bf5bb237","result":{"location":{"lat":31.332012,"lng":121.49158},"address":"上海市宝山区长江南路3号","formatted_addresses":{"recommend":"长江南路地铁站","rough":"长江南路地铁站"},"address_component":{"nation":"中国","province":"上海市","city":"上海市","district":"宝山区","street":"长江南路","street_number":"长江南路3号"},"ad_info":{"nation_code":"156","adcode":"310113","city_code":"156310000","name":"中国,上海市,上海市,宝山区","location":{"lat":31.332012,"lng":121.491577},"nation":"中国","province":"上海市","city":"上海市","district":"宝山区"},"address_reference":{"crossroad":{"id":"6104676","title":"逸仙路/长江南路(路口)","location":{"lat":31.3311,"lng":121.491142},"_distance":104.1,"_dir_desc":"北"},"town":{"id":"310113113","title":"淞南镇","location":{"lat":31.332012,"lng":121.491577},"_distance":0,"_dir_desc":"内"},"street_number":{"id":"13159355764340638544","title":"长江南路3号","location":{"lat":31.331949,"lng":121.491409},"_distance":17.4,"_dir_desc":""},"street":{"id":"3841226627285285649","title":"逸仙路","location":{"lat":31.331884,"lng":121.491745},"_distance":15.9,"_dir_desc":"西"},"landmark_l2":{"id":"10804000183069644061","title":"长江南路地铁站","location":{"lat":31.331793,"lng":121.491425},"_distance":0,"_dir_desc":"内"}}}}
     * @param longitude 经度
     * @param latitude  纬度
     * @return
     */
    public static JSONObject getAddressDetail(BigDecimal longitude, BigDecimal latitude) {
        if (longitude == null || latitude == null) {
            return null;
        }
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>(2);
        params.put("key", "V4MBZ-H4V63-5BK37-YLS6E-XB3RO-7JFRV");
        params.put("location", latitude + "," + longitude);
        String resultStr = restTemplate.getForObject(TENCENT_LBS_URL, String.class, params);
        return JSONObject.parseObject(resultStr);
    }

    /**
     * 根据经纬度获取详细地址
     * 示例：{"nation":"中国","province":"上海市","city":"上海市","district":"宝山区","street":"长江南路","street_number":"长江南路3号"}
     * @param longitude 经度
     * @param latitude  纬度
     * @return
     */
    public static AddressComponent getAddressComponent(BigDecimal longitude, BigDecimal latitude) {
        JSONObject result = getAddressDetail(longitude, latitude);
        if (result != null && result.getInteger("status") == 0) {
            return JSONObject.toJavaObject(result.getJSONObject("result").getJSONObject("address_component"), AddressComponent.class);
        }
        return null;
    }

    /**
     * 根据经纬度获取区域
     * 示例："宝山区"
     * @param longitude 经度
     * @param latitude  纬度
     * @return
     */
    public static String getArea(BigDecimal longitude, BigDecimal latitude) {
        AddressComponent addressComponent = LBSUtil.getAddressComponent(longitude, latitude);
        return addressComponent == null ? null : addressComponent.getDistrict();
    }

    /**
     * 详细地址
     * 示例：{"nation":"中国","province":"上海市","city":"上海市","district":"宝山区","street":"长江南路","street_number":"长江南路3号"}
     * @Author: daqian
     * @Date: 2018/12/5 11:18
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class AddressComponent implements Serializable {
        private static final long serialVersionUID = 1L;

        /**
         * 国家
         */
        private String nation;
        /**
         * 省
         */
        private String province;
        /**
         * 市
         */
        private String city;
        /**
         * 区县
         */
        private String district;
        /**
         * 街道
         */
        private String street;
        /**
         * 门牌号
         */
        private String street_number;
    }

}
